<?php
/**********************************************************
  eCode Marketing System
  Version 0.1
  Coupon Generator Function (/admin/function.coupon_generate.php)
**********************************************************/

/**********************************************************
  Function returns PNG image data on success, and 
  false if the image could not be generated.

  Parameters are largely the same as their respective
  database field names.
**********************************************************/

function coupon_generate(array $offer, array $coupon) {

	/* Attempt to load $coupon['base_image'] (defined in 
	   /include/coupon_config.php) as a GD image object. */
	$img = @imagecreatefrompng($coupon['base_image']['filename']);

	if(!$img) {
		/* If we could not create a coupon image, return false
		   to indicate there was an error. */

		return false;
	} else {

		/* Allocate background and foreground
		   colors for overlaid text. */
		$bgcolor = imagecolorallocate($img, 255, 255, 255);
		$fgcolor = imagecolorallocate($img, 0, 0, 0);

		foreach($offer as $keyname => $param) {
			/* Recurse through passed parameters, checking to see if that type of parameter
			   is defined in /include/coupon_config.php, and if so, altering the format
			   accordingly */

			if(isset($offer[$keyname]) && isset($coupon[$keyname])) {
		
				switch($keyname) {
					case 'offer_value':
						if($offer['offer_type'] == 0 || $offer['offer_type'] == 3) {
							// If offer type is Direct Price (0) or Free (With Dollar Value) (3), display "(a $[value] Value)"
							$text = "(a $" . $param . " value)";
						} else {
							// Otherwise, display "With Coupon"
							$text = "With This Coupon";
						}
						break;
					case 'offer_price':
						if($offer['offer_type'] == 3 || $offer['offer_type'] == 4) {
							// Display "FREE" if offer type is Free (With Dollar Value) (3), or Free (Coupon Required) (4)
							$text = "FREE";
						} else if($offer['offer_type'] == 2) {
							// Display price as a "% Off" if offer type is Percent Off (2)
							$text = round($param, 0) . "% Off";
						} else if($offer['offer_type'] == 1) {
							// Display price as "$ Off" if offer type is Indirect Price (1)
							$text = "$" . $param . " Off";
						} else {
							// Display raw price
							$text = "$" . $param;
						}
						break;
					case 'offer_end_date':
						// Get the expiry date and put it on the coupon
						$text = "HURRY! EXPIRES " . strtoupper(date("F j, Y", strtotime($param))) . "!";
						break;
					default:
						// Pass-through, no format change
						$text = $param;
					}

				if($keyname == 'offer_pos_code') {
					// If we're painting the POS offer number, display it in white inside a black box
					imagefilledrectangle($img, 240, $coupon[$keyname]['ypos'], 280, $coupon[$keyname]['ypos'] + 16, $fgcolor);
					imagettftext($img, $coupon[$keyname]['size'], 0, 242, $coupon[$keyname]['ypos']+12, $bgcolor, $coupon[$keyname]['font'], "# " . $param);

				} else {
					// Center and paint the text on the base image
					$textsize = imagettfbbox($coupon[$keyname]['size'], 0, $coupon[$keyname]['font'], $text);
					$textcenter = (int)($coupon['base_image']['width'] / 2) - (int)($textsize[4] / 2);
					imagettftext($img, $coupon[$keyname]['size'], 0, $textcenter, $coupon[$keyname]['ypos'], $fgcolor, $coupon[$keyname]['font'], $text);
				}
			}
		}
	}
	
	ob_start();
	imagepng($img,null);
	$imagedata = ob_get_contents();
	ob_end_clean();

	imagedestroy($img);
	return $imagedata;
}
?>